package com.leetcode.problems053;

import java.util.ArrayList;
import java.util.List;

/**
 * https://leetcode-cn.com/problems/maximum-subarray/
 * @Date 2021/12/09 14:34
 * @Author ghery
 * @Version V1.0
 */
public class Solution {

    public int maxSubArray(int[] nums) {
        int maxTotal = Integer.MIN_VALUE;
        int[] max = new int[nums.length];
        for (int i = 0; i < nums.length; i++) {
            int maxTemp = nums[i];
            if(i > 0){
                maxTemp = Math.max(maxTemp,max[i - 1] + maxTemp);
            }
            max[i] = maxTemp;
            if(maxTotal < maxTemp){
                maxTotal = maxTemp;
            }
        }
        return maxTotal;
    }



    public static void main(String[] args) {
        int[] nums = {5,4,-1,7,8};
        System.out.println(new Solution().maxSubArray(nums));
    }
}
